package com.app.miwawa.cui.system;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.framework.common.api.query.Criterion.Operator;
import org.framework.common.api.support.model.HtmlModel;
import org.framework.common.api.support.model.QueryModel;
import org.framework.common.cui.AbstractController;
import org.framework.common.ext.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.app.miwawa.api.system.entity.Admin;
import com.app.miwawa.api.system.entity.Center;
import com.app.miwawa.api.system.entity.view.Account;
import com.app.miwawa.api.system.entity.view.AuthsView;
import com.app.miwawa.api.system.service.bo.AccountBo;
import com.app.miwawa.api.system.service.bo.AdminBo;

/**
 * 早教机构用户控制器类
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年2月7日 上午11:53:13
 * @version 1.0.2016
 */
@Controller
public class AccountController extends AbstractController {
	
	@Autowired
	private AccountBo accountBo;
	
	@Autowired
	private AdminBo adminBo;
	
	@Autowired
	private HtmlModel model;
	
	@RequestMapping(value="/login")
	public ModelAndView login(@ModelAttribute final Account entity) throws Exception {
		
		Map<String,String> data = new HashMap<String,String>();
		data.put("mobile",entity.getMobile());
		
		try{
			registerDataSource(null);
			
			Account account = entity.checkMobile(accountBo);
			if (entity.getMobile() == null) {
				data.put("message", "");
				return new ModelAndView("enter","data",data);
			} else if (account == null) {
				data.put("message", "账号错误");
				return new ModelAndView("enter","data", data);
			} else if ((account = entity.checkPassword(accountBo)) == null) {
				data.put("message", "密码错误");
				return new ModelAndView("enter","data", data);
			} else {
				account.setAuths(accountBo);
				
				request.getSession().setAttribute(USER_LOGINED_SESSION, account);
				
				response.sendRedirect("index");
			}
			return null;
			
		} catch (Exception e) {
			data.put("message", "");
			return new ModelAndView("enter","data", data);
		}
	}
	
	@RequestMapping(value="/admin")
	public ModelAndView login(@ModelAttribute final Admin entity) throws Exception {
		
		Map<String,String> data = new HashMap<String,String>();
		data.put("account",entity.getAccount());
		
		try{
			registerDataSource(null);
			
			Admin admin = entity.checkAccount(adminBo);
			
			if(entity.getAccount() == null){
				data.put("message", "");
				return new ModelAndView("manager","data",data);
			}else if(admin == null){
				data.put("message", "账号错误");
				return new ModelAndView("manager","data", data);
			}else if((admin = entity.checkPassword(adminBo)) == null){
				data.put("message", "密码错误");
				return new ModelAndView("manager","data", data);
			}else{
				String centerid = admin.getCenterid();
				if(StringUtil.isNotEmpty(centerid)){
					Center center = adminBo.getCenter(centerid);
					admin.setDbname(center.getDbname());
				}
				request.getSession().setAttribute(USER_LOGINED_SESSION, admin);
				response.sendRedirect("index");
			}
			return null;
		} catch (Exception e) {
			data.put("message", "");
			return new ModelAndView("manager","data", data);
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/{module}/{function}/list")
	public ModelAndView index(@PathVariable final String module, @PathVariable final String function) {
		try {
			Account session = (Account)request.getSession().getAttribute(USER_LOGINED_SESSION);
			if (session == null) {
				response.sendRedirect("login.html");
			} else {
				registerDataSource(null);
				
				String auths = request.getParameter("auths");
				
				QueryModel query = new QueryModel(AuthsView.class, 0, 0);
				query.addAndQuery("pcode", auths, Operator.EQ);
				query.addAndQuery("usersid", session.getId(), Operator.EQ);
				
				List<AuthsView> result = (List<AuthsView>)bo.findByQuery(query).getRows();
				model.addResult("auths", result);
				
				return new ModelAndView(module+"/"+function+"/list", "data", model);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
